package edu.kit.aifb.ldsbench.generator.opt2;

import java.util.List;
import java.util.Map;

import org.openrdf.model.URI;
import org.openrdf.repository.RepositoryConnection;

import edu.kit.aifb.ldsbench.cli2.CLI;
import edu.kit.aifb.ldsbench.generator.opt.util2.SesameQuery;
import edu.kit.aifb.ldsbench.generator.util2.IDistributionValues;
import edu.kit.aifb.ldsbench.out2.IGeneratorOutput;
import edu.kit.aifb.ldstream.learner.tasks.mod.ModStatistics;

public class PropertyOptimizer {

	private OptimizationSetter setter;
	private OptimizationEvaluator evaluator;
	private SesameQuery sesameQuery;
	
	
	public PropertyOptimizer(RepositoryConnection con,
			IDistributionValues distribValues, 
			IOptimizationSolver optSolver,
			ModStatistics stats) {
		
		this.sesameQuery = new SesameQuery(con);
		this.setter = new OptimizationSetter(distribValues, optSolver, sesameQuery, stats);
		this.evaluator = new OptimizationEvaluator(sesameQuery, distribValues);
	}
	
	
	public void optimizePropertyOutput(double eventSize, Map<URI, List<URI>> generationInstances, IGeneratorOutput out){
		if(eventSize < 1 || generationInstances == null){
			throw new IllegalArgumentException("Invalid parameters for optimizing properties");
		}
		OptimizationResult result = this.setter.setUpOptimizationProblem(eventSize, generationInstances);
		if(result != null){
			this.evaluator.setOutput(out);
			this.evaluator.evaluate(result, generationInstances);
		}else{
			CLI.log.warning("OptimisationProblem could not find a solution");
		}
	}
	
	public void shutdown(){
		this.sesameQuery.close();
	}
	
	
	
}
